/**************************************************************************
 *		   Copyright(c)	2003 by	Sunplus	Technology Co.,	Ltd.			 *
 *																		  *
 *	This software is copyrighted by	and	is the property	of Sunplus		  *
 *	Technology Co.,	Ltd. All rights	are	reserved by	Sunplus	Technology	  *
 *	Co., Ltd. This software	may	only be	used in	accordance with	the		  *
 *	corresponding license agreement. Any unauthorized use, duplication,	  *
 *	distribution, or disclosure	of this	software is	expressly forbidden.  *
 *																		  *
 *	This Copyright notice "M U S T"	not	be removed or modified without	  *
 *	prior written consent of Sunplus Technology	Co., Ltd.				  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd. reserves the right	to modify this		  *
 *	software without notice.											  *
 *																		  *
 *	Sunplus	Technology Co.,	Ltd.										  *
 *	19,	Innovation First Road, Science-Based Industrial	Park,			  *
 *	Hsin-Chu, Taiwan, R.O.C.											  *
 *																		  *
 *	Author:	Ted	Chiu													  *
 **************************************************************************/
#include "general.h"
#include "disp_api.h"
#if 0
/* ENABLE POWER CTRL FOR AU AN01? */
#define SUPPORT_AN02_POWER_CTRL ((PANEL_TYPE_LCD==A015AN02_UPS051) || (PANEL_TYPE_TV==A015AN02_UPS051) \
                             ||  (PANEL_TYPE_LCD==A015AN02_UPS052) || (PANEL_TYPE_TV==A015AN02_UPS052))

#endif
/* R3, bit 2:0 */
#define	A020CN01_SEL_051			0x0
#define	A020CN01_SEL_052			0x1
#define	A020CN01_SEL_052_D1		0x2
#define	A020CN01_SEL_YUV_A		0x3
#define	A020CN01_SEL_YUV_A_27	0x4
#define	A020CN01_SEL_YUV_B		0x5
#define	A020CN01_SEL_YUV_B_27	0x6
#define	A020CN01_SEL_656			0x7

UINT8 A020CN01_color[4] = { 0, 0, 0, 0 };

void A020CN01_Init(UINT8 mode) 
{
	UINT8 sel = 0;

	dispSyncWidthSet(1,3);

	switch (mode) {
		case UPS051:
			dispPnlVhSet(617,262,101,13,480,240);
			dispClkDivSet(96, 10);
			dispColorSet(3);
			sel = A020CN01_SEL_051;
			break;
			
		case UPS052:
			dispPnlVhSet(390,262,64,13,320,24);
			dispClkDivSet(96, 4);
			sel = A020CN01_SEL_052;
			break;
			
		case CCIR601_NTSC_8:
			dispPnlVhSet(858,525,126,22,720,480);
			dispClkDivSet(27, 1);
			dispColorSet(0);
			sel = A020CN01_SEL_YUV_A_27;
			break;
			
		default:
			break;
	}

	dispEn(1);
	dispVsyncWait(DISP_SIGNAL_RISING);
	dispSpiCtrl(0x000F, 16, 0);
	dispSpiCtrl(0x4003, 16, 0);
	dispSpiCtrl(0x6000 + sel, 16, 0);
	dispSpiCtrl(0xC006, 16, 0);
}

void A020CN01_Flip(UINT8 mode) 
{
	UINT16 regvalue = 0x4003;
	mode &= 0x03;
	if(mode & DISP_FLIP_H)	{	/* Left/Right Flipped */
		regvalue &= ~0x01;
	} 
	if(mode & DISP_FLIP_V)	{	/* Up/Down Flipped */
		regvalue &= ~0x02;
	}
	dispSpiCtrl(regvalue, 16, 0);

	if((XBYTE[0x2D00] & 0x0F) == UPS051) {
		dispColorSet(A020CN01_color[mode]);
	}
}

//=============================================================================
//dispSpiCtrl()
//=============================================================================
static void dispSpiCtrl( UINT32 InputData, UINT8 BitLength, UINT8 LowBitFirst) 
{
       UINT8 i;        /* Load = GPIO12 ; SCLK = GPIO11 ; SDAT: GPIO9 */
       UINT32 indata;

       if(LowBitFirst ) {
              indata = InputData;
       } else {
              indata = 0;

		i = 24;
		do {
                     if(InputData & 0x800000 ) {
                            indata |= (0x0001 << i);
                     }
                     InputData <<= 1;
		} while (--i);

              indata >>= (24 - BitLength);
       }

	XBYTE[0x2D73]|= 0x38;  // CSb, SCL SDA all high  // 20040512 mattwang modify
	XBYTE[0x2D73]&= 0xDF;  // Csb low
	XBYTE[0x2D76]|= 0x38;  // set digtv[21:19] output mode  // 20040512 mattwang modify

	do {
              if(indata & 0x8000 ) {
                     XBYTE[0x2D73] |= 0x08;  // SDA high
              } else {
                     XBYTE[0x2D73] &= 0xF7;  // SDA low
              }
              XBYTE[0x2D73] |= 0x10;  // SCL high
              XBYTE[0x2D73] &= 0xEF;  // SCL low

              indata = indata << 1;
	} while (--BitLength);

       XBYTE[0x2D73] |= 0x20;  // CSb high
}

